<?php

#require_once('Imaging.php');

// Mode 4:Proportional Resize, zoom, crop
// Mode 5:Crop to Fit + zoom (obsoleted)
// Mode 6:Proportional Resize, zoom, crop + bg

class Imaging_ResizeMode4 extends Imaging {  
  public $zoom_factor    = 100;
  public $x_crop_factor  = 50; // 0 top  <-> bottom 100
  public $y_crop_factor  = 50; // 0 left <-> right  100

  function set_param($h_param = array()){
    if (isset($h_param['z']))     $this->zoom_factor  =$h_param['z']; # zoom
    if (isset($h_param['x']))     $this->x_crop_factor=$h_param['x'];   #x crop
    if (isset($h_param['y']))     $this->y_crop_factor=$h_param['y'];   #y crop
  }
  function get_param($mode){
  	if ($mode != 'as_string') return '';
  	// as string
  	return 'z:'.$this->zoom_factor.'|x:'.$this->x_crop_factor.'|y:'.$this->y_crop_factor;
  }
  
  function resize($max_w, $max_h){
    // Resize
    $src_w = $this->get_width();
    $src_h = $this->get_height();
    $zoom_factor   = $this->zoom_factor / 100;
    $x_crop_factor = $this->x_crop_factor / 100;
    $y_crop_factor = $this->y_crop_factor / 100;
    

    //echo("$src_w * $zoom_factor ".($src_w * $zoom_factor)."> $max_w\n");
    //echo("$src_h * $zoom_factor ".($src_h * $zoom_factor)."> $max_h\n");    
    if($src_w * $zoom_factor > $max_w || $src_h * $zoom_factor > $max_h){
      $a= $max_w / $max_h;
      $b= $src_w / $src_h;

      if ( $a < $b ){ // source is 'wider' ( in term of w/h ratio)
        list($resize_w, $resize_h) = $this->resize_to_width($zoom_factor * $max_w );
        if ($resize_w > $max_w || $resize_h > $max_h){
          $this->crop( $resize_w - $max_w > 0 ? ($resize_w - $max_w)*$x_crop_factor : 0,
                       $resize_h - $max_h > 0 ? ($resize_h - $max_h)*$y_crop_factor : 0, 
                       $resize_w > $max_w ? $max_w : $resize_w, 
                       $resize_h > $max_h ? $max_h : $resize_h);
        }       
        //$this->composite_background($max_w, $max_h, $bg_color);
      } else {
        list($resize_w, $resize_h) = $this->resize_to_height($zoom_factor * $max_h);
        if ($resize_w > $max_w || $resize_h > $max_h){
          $this->crop( $resize_w - $max_w > 0 ? ($resize_w - $max_w)*$x_crop_factor : 0,
                       $resize_h - $max_h > 0 ? ($resize_h - $max_h)*$y_crop_factor : 0, 
                       $resize_w > $max_w ? $max_w : $resize_w, 
                       $resize_h > $max_h ? $max_h : $resize_h);
        }
        //$this->composite_background($max_w, $max_h, $bg_color);
      }
      
      $this->resized = TRUE;
      return $this->get_dimension();
    } 
    
    $this->resized = FALSE;
    return $this->get_dimension();

  } // end resize function
  
}
?>